package org.bingmaps.app;

import java.lang.reflect.Method;
import java.util.Calendar;

import locationServices.GetGpsCoordinate;
import locationServices.MyLocationListener;

import General.GlobalValueClass;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class WhatToTrack extends Activity {

	static Context context;
	static String TrackingType = new String();

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.what_to_track);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.what_to_track, menu);
		return true;
	}
	
	public void StartTrackCar(View view){Connectivity_GPS(2000);  GlobalValueClass.SetTrackingType(new String("Car"));} //On car
	public void StartTrackBicycle(View view){Connectivity_GPS(5000);   GlobalValueClass.SetTrackingType(new String("Bicycle"));} //On bicycle
	public void StartTrackFoot(View view){ Connectivity_GPS(20000);  GlobalValueClass.SetTrackingType(new String("Foot"));} //On foot

	
	//Sensor Managing
    void Connectivity_GPS(int Frequency){
    
    	boolean GPS;

    	LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
		new Intent("android.location.GPS_ENABLED_CHANGE");
		
		isOnline(getApplicationContext());
	 	
    	//GPS 
    	GPS = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
    	
     // Check if enabled and if not send user to the GPS settings
        if (!GPS)
        	msbox("GPS","Please enable your GPS! Do you want to enable it?");
        else
        {
        	        	
	       	//Starts assync task to get coordinates
        	context = getApplicationContext();
	       	Intent i = new Intent(context, NowTracking.class);
	       	String s= new String();
	       	s+=Frequency;
	       	i.putExtra("Frequency", s);
     	  	startActivity(i);
     	  	finish();
        }
        
    }
    
    public boolean isOnline(Context context) { 

    	WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(getApplicationContext().WIFI_SERVICE); 
        
        try { 
                ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
                
                if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) { 
                	Log.d("####","Conected to WIFI Internet"); 
                    Log.d("####","Connection status Wifi: "+cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected());
                        return true; 
                } 
                else{
	             // turn on wifi
	            	if(!wifiManager.isWifiEnabled()){
	            		wifiManager.setWifiEnabled(true);
	            	}
	            	
	                if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) { 
	                	Log.d("####","Conected to WIFI Internet"); 
	                    Log.d("####","Wifi connection status: "+cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected());
	                       return true; 
	                } else {
	                	
	           
	                	if(cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()){ 
	                	
	                	 Log.d("####", "Conected to 3G Internet"); 
	                     Log.d("####","3G connection status: " + cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected());
	                     	return true; 
	                	} 
	                	else { 
	                		
	                		//turn on 3G
	                		//ConnectivityManager mgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
	                		//Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
	                		//dataMtd.setAccessible(true);
	                		//dataMtd.invoke(mgr, true); 
	                		
	                			//test again
	                		if(cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()){ 
	                        	
	                		Log.d("####", "Conected to 3G Internet"); 
	   	                    Log.d("####","3G connection status: " + cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected());
	   	                     	return true; 
	                       	} 
	                		else
	                		{
	                			Log.e("####","Wifi connection status: "+cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()); 
	                            Log.e("####","Wifi connection status: : "+cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()); 
	                            return false; 
	                		}
	                	} 
	                }
              }
        } 

        catch (Exception e) { 
                Log.e("####", "Error on connecting to the Internet: " + e.getMessage()); 
                return false; 
        } 
    } 
    
    
    //Message BOX
    public void msbox(String str,String str2)
    {
    	
        AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this); 
        dlgAlert.setTitle(str); 
        dlgAlert.setMessage(str2);
        
        dlgAlert.setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        });
        dlgAlert.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            	Intent intentGPS = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            	startActivity(intentGPS);  
            }
        });
        dlgAlert.setCancelable(true);
        dlgAlert.create().show();
    }
	
	
}
